<?php

/**
 * Class E_OrderList 企业订单列表资源
 */

namespace V1;
class E_OrderList extends \ResourceClass
{
    public $isActive = true;
    public $remark = '获取企业订单列表';

/**
 * 获取列表方法
 * @param array $req
 * @param array $post
 * @param $result
 */
    public function get(array $req, array $post, &$result){
        try {
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }
            if(!array_key_exists('enterpriseId',$req)&&empty($req['enterpriseId'])){
                return 'essential data required';
            }
            if (\Biz\UserHandler::checkAuth($req['userType'],$req['uid'],$req['enterpriseId'],$req['t'],$req['k']) == false) {
                return 'data illegal';
            }
            $l = array_key_exists('l',$req)?$req['l']:100000;
            $o = array_key_exists('o',$req)?$req['o']:0;
            $tabulation = new \Cls\Tabulation();
            // 初始化列表类相关参数
            if(array_key_exists('ordercode',$req)&&!empty($req['ordercode'])){
                $tabulation->setWhere('ordercode', $req['ordercode']);
            }
            $tabulation->setWhere('enterprise_id', $req['enterpriseId'])
                ->setWhere('status', 'in','1,2,3,5,6')
                ->setOffset($o)
                ->setLimit($l)
                ->setOrderBy('id', 'desc');
            $order_handler = new \Biz\OrderHandler();
            $order_handler->getOrderListByEnterpriseId($tabulation);
            $order_info = array();
            if($tabulation->getSuccessStatus()){
                foreach($tabulation->getData() as $order_cls){
                    $order_tmp = array();
                    $order_tmp['id'] = $order_cls->getId();
                    $order_tmp['ordercode'] = $order_cls->getCode();
                    $order_tmp['enterprise_id'] = $order_cls->getEnterpriseId();
                    $order_tmp['enterprise_customer_id'] = $order_cls->getEnterpriseCustomerId();
                    $order_tmp['enterprise_channel_id'] = $order_cls->getEnterPriseChannelId();
                    $order_tmp['status'] = $order_cls->getStatus();
                    $order_tmp['contact'] = $order_cls->getContact();
                    $order_tmp['tel'] = $order_cls->getTelephone();
                    $order_tmp['price'] = $order_cls->getPrice();
                    $order_tmp['discount'] = $order_cls->getDiscount();
                    $order_tmp['coupon'] = $order_cls->getConupon();
                    $order_tmp['need_pay_price'] = $order_cls->getNeedPayPrice();
                    $order_tmp['pay_status'] = $order_cls->getPayStatus();
                    $order_tmp['pay_datetime'] = $order_cls->getPayTime();
                    $order_tmp['pay_message'] = $order_cls->getPayMessage();
                    $order_tmp['delivery_status'] = $order_cls->getDeliveryStatus();
                    $order_tmp['delivery_charge'] = $order_cls->getDeliveryCharge();
                    $order_tmp['delivery_time'] = $order_cls->getDeliveryTime();
                    $order_tmp['delivery_province_id'] = $order_cls->getDeliveryProvinceId();
                    $order_tmp['delivery_city_id'] = $order_cls->getDeliveryCityId();
                    $order_tmp['delivery_address'] = $order_cls->getDeliveryAddress();
                    $order_tmp['remark'] = $order_cls->getRemark();
                    $order_tmp['active_discount'] = $order_cls->getActiveDiscount();
                    $order_tmp['insert_time'] = $order_cls->getInsertTime();
                    $order_tmp['update_time'] = $order_cls->getUpdateTime();
                    $order_tmp['is_pay_type'] = $order_cls->getIsPayType();
                    $order_tmp['pay_type_item_id'] = $order_cls->getPayTypeItemId();
                    $order_tmp['transaction_id'] = $order_cls->getTransactionId();
                    $order_tmp['status_string'] = $order_cls->getStatusStr();
                    $order_tmp['pay_status_string'] = $order_cls->getPayStatusStr();
                    $order_tmp['delivery_status_string'] = $order_cls->getDeliveryStatusStr();
                    if(is_object($order_cls) && $order_cls->getEnterpriseCustomerInfo() != false){
                        $order_tmp['company'] = $order_cls->getEnterpriseCustomerInfo()->getCompany();
                    }else{
                        $order_tmp['company'] ='';
                    }
                    $order_info['lists'][] = $order_tmp;
                }
            }
            $total = $tabulation->getTotal();
            $order_info['total'] = $total;
            $items = self::getFiled($order_info);
            $result = $items?$items:array();
        } catch (Exception $e){
            return $e->getMessage();
        }
    }
    //字段转换
    private static function getFiled($result){
        if($result){
            foreach($result['lists'] as $k=>$v){
                //判断是什么状态的订单
                $refund_order_handler = new \Biz\RefundOrderHandler();
                $refund_order_cls = $refund_order_handler->getByIdAndEnterpriseIdAndStatus($v['id'],$v['enterprise_id']);
                if($refund_order_cls === true){
                    $result['lists'][$k]['type'] = '2';
                }else{
                    $result['lists'][$k]['type'] = '1';
                }
            }
            return $result;
        }
    }
}



?>